package com.changgou.order.controller;

import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Statement;
import java.util.List;

/**
 * @ClassName CartController
 * @Description
 * @Author Mr.L
 * @Date 22:51 2019/11/23
 * @Version 2.1
 **/
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 将商品添加到购物车中
     * @param id
     * @param num
     * @return
     */
    @RequestMapping("/add")
    public Result add(Long id , Integer num){
//        String username = "wangwu";
        String username = TokenDecode.getUserInfo().get("username");
        cartService.add(num, id, username);
        return new Result(true, StatusCode.OK,"已添加至购物车");
    }

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<OrderItem>> list(){
//        String username = "wangwu";
        String username = TokenDecode.getUserInfo().get("username");
        List<OrderItem> orderItems = cartService.list(username);
        return new Result<>(true,StatusCode.OK,"购物车列表查询成功", orderItems);
    }

}
